'use strict'

export const errTips = {
  showMesg: (msg) => {
    wx.hideToast()
    wx.showToast({ title: msg || '', icon: 'none', duration: 3000 })
  }
}

export const tools = {
  showTips: (msg) => {
    wx.hideToast()
    wx.showToast({ title: msg || '', icon: 'none', duration: 2000 })
  },
  setCacheSync: function (key, val) {
    try {
      wx.setStorageSync(key, val)
    } catch (e) {
      wx.showModal({
        title: '提示',
        content: '微信缓存出现异常！' + e.message,
        showCancel: false,
        cancelText: '知道了'
      })
    }
  },
  getCacheSync: function (key) {
    try {
      return wx.getStorageSync(key)
    } catch (e) {
      wx.showModal({
        title: '提示',
        content: '微信缓存出现异常！' + e.message,
        showCancel: false,
        cancelText: '知道了'
      })
    }
  },
  clearCacheSync: function () {
    try {
      wx.clearStorageSync()
    } catch (e) {
      wx.showModal({
        title: '提示',
        content: '微信缓存出现异常！' + e.message,
        showCancel: false,
        cancelText: '知道了'
      })
    }
  },
  secondToTimeStamp (dura) {
    let min = Math.floor(dura / 60)
    let sec
    if (min > 0) {
      sec = Math.floor(dura % 60)
    } else {
      sec = Math.floor(dura)
    }
    let minstr = min < 10 ? '0' + min : min + ''
    let secstr = sec < 10 ? '0' + sec : sec + ''
    return minstr + ':' + secstr
  },
  isEmptyObject (obj) {
    var name
    for (name in obj) {
      return false
    }
    return true
  },
  isChinese (str) {
    var reg = /^(?:[\u3300-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F]|[\uD840-\uD868\uD86A-\uD872][\uDC00-\uDFFF]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD873[\uDC00-\uDEAF]|\uD87E[\uDC00-\uDE1F])+$/m
    return reg.test(str)
  },
  needAuthorizeScope (scope, cb) {
    wx.getSetting({
      success: function (data) {
        if (data.authSetting[scope] === false) {
          console.log('用户拒绝过授权')
          wx.showModal({
            title: '用户未授权',
            content: '请选中“录音功能”，然后点击确定即可正常使用。',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.openSetting({
                  success: function success (res) {
                    if (res.authSetting[scope] === true) {
                      console.log('用户已授权')
                      wx.showToast({ title: '设置成功！' })
                    }
                  }
                })
              }
            }
          })
        } else {
          console.log('第一次授权')
          wx.authorize({
            scope: scope,
            success () {
              cb && cb()
            },
            fail () {
              wx.showToast({
                title: '你取消了授权，请重试！',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  },
  compareVersion (v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)

    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }

    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])

      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }

    return 0
  },
  get (key) {
    let data = wx.getStorageSync(key)
    if (!data) {
      return null
    } else {
      let now = new Date().getTime()
      if (!data.expired) return null
      if (now >= parseInt(data.expired)) {
        wx.removeStorageSync(key)
        return null
      } else {
        return data.data
      }
    }
  },
  set (key, data, expired) {
    let time = new Date().getTime() + expired
    wx.setStorageSync(key, { data: data, expired: time })
  },
  remove (key) {
    wx.removeStorageSync(key)
  }
}
